import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import { addBlog, createShop, shopState } from "../../utils/api";
import { checkIdNumberValid } from "../../utils/util";
let tempImageArr: any = [];
let tempShopImage: any = [];

let tempImageReadyArr: any = [];
let logoImage: any = null;
let shopImage: any = [];
let addressObj: any = {};
let index = 0;
let baseURL = "http://192.168.0.101:3000";
Page({
  data: {
    userInfo: 0,
    auditRes: null,
    addres: '',
    imageFileList: [],
    shopLogoImage: null,
    shopImage: [],
    shopManagerActiveIndex: 0,
    shopManager: [
      {
        desc: '身份验证',
      },
      {
        desc: '店铺信息完善',
      },
      {
        desc: '审核结果',
      }
    ],
    nameValue: "",
    carID: "",
    addressInfo: "",
    shopName: "",
    shopNameErrorMessage: "",
    addressInfoErrorMessage: "",
    nameErrorMessage: "",
    carIDErrorMessage: ""
  },
  onLoad() {
    const that = this;
    wx.getStorage({
      key: "userInfo",
      success(res) {
        that.setData({
          userInfo: res.data.userPermissions
        })
      }
    })
    shopState().then(res => {
      if (res.data) {
        this.setData({
          shopManagerActiveIndex: 2
        })
      }
      this.setData({
        auditRes: res
      })
    })
  },
  // 图片上传前验证
  beforeReadImage(event: any) {
    const { file, callback } = event.detail;
    index = 0;
    this.setData({
      imageFileList: []
    })
    if (Array.isArray(file)) {
      file.forEach((element: any) => {
        if (element.type === 'image') {
          callback(true)
        } else {
          Toast.fail('文件格式不符合');
        }
      });
    } else {
      if (file.type === 'image') {
        callback(true)
      } else {
        Toast.fail('文件格式不符合');
      }
    }

  },
  readyImage(event: any) {
    tempImageReadyArr = [];
    tempImageArr = event.detail.file;
    this.setData({
      imageFileList: tempImageArr
    })
    this.afterReadImage();
  },
  // 图片上传
  afterReadImage() {
    const element = tempImageArr[index];
    if (!element) {
      return;
    }
    this.imageUploadFn(element.url);
  },
  imageUploadFn(url: any) {
    wx.uploadFile({
      url: `${baseURL}/api/file/uploadImage`,
      filePath: url,
      name: 'image',
      success: (res: any) => {
        let data = JSON.parse(res.data);
        tempImageReadyArr.push({
          thumb: data.data[0].thumbnail,
          url: data.data[0].path
        });
        index++;
        this.afterReadImage();
      }
    });
  },
  synchronousSetNameData(msg: any) {
    if (!msg.detail) {
      this.setData({
        nameErrorMessage: "姓名不允许为空",
        nameValue: ""
      })
    }

    if (msg.detail) {
      this.setData({
        nameErrorMessage: "",
        nameValue: msg.detail
      })
    }
  },
  synchronousSetCarIdNameData(msg: any) {
    if (!msg.detail) {
      this.setData({
        carIDErrorMessage: "省份证号不允许为空",
        carID: ""
      })
    }
    if (!checkIdNumberValid(msg.detail)) {
      this.setData({
        carIDErrorMessage: "省份证号不符合规则",
        carID: ""
      })
    }
    if (checkIdNumberValid(msg.detail)) {
      this.setData({
        carIDErrorMessage: "",
        carID: msg.detail
      })
    }
  },
  synchronousSetAddressData(msg: any) {
    if (!msg.detail) {
      this.setData({
        addressInfoErrorMessage: "地址不允许为空",
        addressInfo: ""
      })
    }

    if (msg.detail) {
      addressObj["detaile"] = msg.detail;
      this.setData({
        addressInfoErrorMessage: "",
        addressInfo: msg.detail
      })
    }
  },
  synchronousSetShopNameData(msg: any) {
    if (!msg.detail) {
      this.setData({
        shopNameErrorMessage: "店铺名称不允许为空",
        shopName: ""
      })
    }

    if (msg.detail) {
      this.setData({
        shopNameErrorMessage: "",
        shopName: msg.detail
      })
    }
  },
  // 返回首页重新填写
  onGoHomePage() {
    this.setData({
      shopManagerActiveIndex: 0
    })
  },
  // 下一步1
  onNextPage() {
    if (!this.data.nameValue) {
      Toast.fail('姓名错误');
      return
    }
    if (this.data.nameValue.length > 5) {
      Toast.fail('姓名错误');
      return
    }
    if (!this.data.carID) {
      Toast.fail('省份证号错误');
      return
    }
    if (!tempImageReadyArr.length) {
      Toast.fail('上传省份证照片错误');
      return
    }
    this.setData({
      shopManagerActiveIndex: 1
    })
  },
  // 上传logo图片
  uploadLogoImage(event: any) {
    const e = event.detail.file;
    this.setData({
      shopLogoImage: e
    })
    wx.uploadFile({
      url: `${baseURL}/api/file/uploadImage`,
      filePath: e.url,
      name: 'image',
      success: (res: any) => {
        let data = JSON.parse(res.data);
        logoImage = data.data[0].thumbnail;
      }
    });
  },
  // 上传店铺图片
  uploadShopImage(event: any) {
    shopImage = [];
    tempShopImage = [];
    tempShopImage = event.detail.file;
    this.setData({
      shopImage: tempShopImage
    })
    this.uploadShopImageLoop();
  },
  uploadShopImageLoop() {
    if (!tempShopImage[index]) {
      console.log(shopImage);
      return;
    }
    this.uploadShopImageServe(tempShopImage[index]);
  },
  uploadShopImageServe(e: any) {
    wx.uploadFile({
      url: `${baseURL}/api/file/uploadImage`,
      filePath: e.url,
      name: 'image',
      success: (res: any) => {
        let data = JSON.parse(res.data);
        shopImage.push({
          thumb: data.data[0].thumbnail,
          url: data.data[0].path
        });
        index++;
        this.uploadShopImageLoop();
      },
      fail: (err) => {
        console.log(err);
      }
    });
  },
  // 获取当前位置
  getLoaction() {
    wx.chooseLocation({
      success: (e) => {
        addressObj["address"] = e.address;
        addressObj["name"] = e.name;
        addressObj["latitude"] = e.latitude;
        addressObj["longitude"] = e.longitude;
        this.setData({
          addres: e.name
        })
      }
    })
  },
  // 提交审核
  createShop() {
    if (!this.data.shopName) {
      Toast.fail('店铺名称不允许为空');
      return
    }
    if (!this.data.addressInfo) {
      Toast.fail('定位不允许为空');
      return
    }
    if (!this.data.addressInfo) {
      Toast.fail('详细地址不允许为空');
      return
    }
    if (shopImage.length == 0) {
      Toast.fail('店铺图片不允许为空');
      return
    }
    if (!logoImage) {
      Toast.fail('店铺LOGO不允许为空');
      return
    }
    createShop({
      realName: this.data.nameValue,
      identityCard: this.data.carID,
      shopAuditImage: JSON.stringify(tempImageReadyArr),
      shopName: this.data.shopName,
      shopSignatureImage: logoImage,
      shopImage: JSON.stringify(shopImage),
      shopAddress: addressObj
    }).then(res => {
      this.setData({
        shopManagerActiveIndex: 2
      })
      if (res) {
        this.onLoad();
      }
    })
  }
})